/***
 * Tree
 * @param root
 * @constructor Tree
 */
var Tree = function (root) {
	this.root = root || new Node();
};

Tree.prototype = {
	constructor: Tree,
	isEmpty: function () {
		return this.root === undefined;
	},
	/***
	 *前序
	 */
	preorder: function (visit) {
		var root = this.root;

		(function f(node) {
			var childs = node.childs;
			console.log(node.data.title || {});
			$.isFunction(visit) && visit(node);

			if (!node.isLeaf()) {
				for (var i = 0, len = childs.length; i < len; i++) {
					node = f(childs[i]);
				}
			}
		}(root));
	},
	/**
	 * 层次
	 * @param visit
	 */
	levelPrint: function (visit) {
		var queue = [], root = this.root, node;

		(function () {
			if (root !== undefined) {
				queue.push(root);
			}

			while (queue.length !== 0) {
				node = queue.shift();
				$.isFunction(visit) && visit(node);
				queue = queue.concat(node.childs);
			}
		}());
	}
};

